/*
 * This calendar application is brought to you
 * by Group 3 of User Interface Programming II, vt11.
 *
 * Group Members:
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import vical.model.Entry;

/**
 * This is a label that shows entry
 * and open the entry in Entrydialog
 * to show details, be edited or deleted
 * @author Pooya
 */
class JEntryLabel extends JLabel {

    private Entry entry;
    private boolean mouseOver;
    private DayCard dayCard;

    public JEntryLabel(DayCard dayCard, Entry entry) {
        this.dayCard = dayCard;
        this.entry = entry;
        this.setText("•" + this.entry.getName());
        mouseOver = false;
        this.setToolTipText(entry.getTaskDetailedString());


        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                JEntryLabel me = (JEntryLabel) e.getSource();
                // Open the entry in Dialog to edit or delete
                EntryDialog entryDialog = new EditEntryDialog(me.getEntry());
                entryDialog.setVisible(true);
                JEntryLabel.this.dayCard.getMonthView().notifyEntryChanged();
                JEntryLabel.this.dayCard.getMonthView().refresh();
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
                // show it as active
                JEntryLabel me = (JEntryLabel) e.getSource();
                me.setForeground(Color.blue);
                mouseOver = true;
                repaint();
            }

            public void mouseExited(MouseEvent e) {
                // show it as regular
                JEntryLabel me = (JEntryLabel) e.getSource();
                me.setForeground(Color.black);
                mouseOver = false;
                repaint();
            }
        });
    }

    public Entry getEntry() {
        return entry;
    }

    @Override
    public void paint(Graphics g) {
        Rectangle r;
        super.paint(g);
        r = g.getClipBounds();
        if (mouseOver) {
            // Draws a line under the label
            g.drawLine(0, r.height - getFontMetrics(getFont()).getDescent() + 2, getFontMetrics(getFont()).stringWidth(getText()), r.height - getFontMetrics(getFont()).getDescent() + 2);
        }
    }
}
